<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>KWIK.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="KWIK code in Java">
<meta NAME="TITLE" CONTENT="KWIK code in Java">
<meta NAME="KEYWORDS" CONTENT="KWIK,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>KWIK.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "KWIK.java">KWIK.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac KWIK.java</span>
<span class="comment"> *  Execution:    java KWIK file.txt</span>
<span class="comment"> *  Dependencies: StdIn.java StdOut.java In.java SuffixArray.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/63suffix/tale.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Keyword-in-context search.</span>
<span class="comment"> *</span>
<span class="comment"> *  %  java KWIK tale.txt 15</span>
<span class="comment"> *  majesty</span>
<span class="comment"> *   most gracious majesty king george th</span>
<span class="comment"> *  rnkeys and the majesty of the law fir</span>
<span class="comment"> *  on against the majesty of the people </span>
<span class="comment"> *  se them to his majestys chief secreta</span>
<span class="comment"> *  h lists of his majestys forces and of</span>
<span class="comment"> *</span>
<span class="comment"> *  the worst</span>
<span class="comment"> *  w the best and the worst are known to y</span>
<span class="comment"> *  f them give me the worst first there th</span>
<span class="comment"> *  for in case of the worst is a friend in</span>
<span class="comment"> *  e roomdoor and the worst is over then a</span>
<span class="comment"> *  pect mr darnay the worst its the wisest</span>
<span class="comment"> *  is his brother the worst of a bad race </span>
<span class="comment"> *  ss in them for the worst of health for </span>
<span class="comment"> *   you have seen the worst of her agitati</span>
<span class="comment"> *  cumwented into the worst of luck buuust</span>
<span class="comment"> *  n your brother the worst of the bad rac</span>
<span class="comment"> *   full share in the worst of the day pla</span>
<span class="comment"> *  mes to himself the worst of the strife </span>
<span class="comment"> *  f times it was the worst of times it wa</span>
<span class="comment"> *  ould hope that the worst was over well </span>
<span class="comment"> *  urage business the worst will be over i</span>
<span class="comment"> *  clesiastics of the worst world worldly </span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">KWIK</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides a {</span><span class="type">@link</span><span class="comment"> SuffixArray} client for computing</span>
<span class="comment"> *  all occurrences of a keyword in a given string, with surrounding context.</span>
<span class="comment"> *  This is known as </span><span class="keyword">&lt;em&gt;</span><span class="comment">keyword-in-context search</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/63suffix"</span><span class="keyword">&gt;</span><span class="comment">Section 6.3</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">KWIK</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">// Do not instantiate.</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">KWIK</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads a string from a file specified as the first</span>
<span class="comment">     * command-line argument; read an integer k specified as the</span>
<span class="comment">     * second command line argument; then repeatedly processes</span>
<span class="comment">     * use queries, printing all occurrences of the given query</span>
<span class="comment">     * string in the text string with k characters of surrounding</span>
<span class="comment">     * context on either side.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">In</span><span class="normal"> in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> context </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>

<span class="normal">        </span><span class="comment">// read in text</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> text </span><span class="symbol">=</span><span class="normal"> in</span><span class="symbol">.</span><span class="function">readAll</span><span class="symbol">().</span><span class="function">replaceAll</span><span class="symbol">(</span><span class="string">"</span><span class="specialchar">\\</span><span class="string">s+"</span><span class="symbol">,</span><span class="normal"> </span><span class="string">" "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> text</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// build suffix array</span>
<span class="normal">        </span><span class="usertype">SuffixArray</span><span class="normal"> sa </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SuffixArray</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// find all occurrences of queries and give context</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">StdIn</span><span class="symbol">.</span><span class="function">hasNextLine</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">String</span><span class="normal"> query </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> sa</span><span class="symbol">.</span><span class="function">rank</span><span class="symbol">(</span><span class="normal">query</span><span class="symbol">);</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> from1 </span><span class="symbol">=</span><span class="normal"> sa</span><span class="symbol">.</span><span class="function">index</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> to1   </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="normal">N</span><span class="symbol">,</span><span class="normal"> from1 </span><span class="symbol">+</span><span class="normal"> query</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">());</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">query</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">from1</span><span class="symbol">,</span><span class="normal"> to1</span><span class="symbol">)))</span><span class="normal"> </span><span class="keyword">break</span><span class="symbol">;</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> from2 </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> sa</span><span class="symbol">.</span><span class="function">index</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> context</span><span class="symbol">);</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> to2   </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="normal">N</span><span class="symbol">,</span><span class="normal"> sa</span><span class="symbol">.</span><span class="function">index</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> context </span><span class="symbol">+</span><span class="normal"> query</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">());</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">from2</span><span class="symbol">,</span><span class="normal"> to2</span><span class="symbol">));</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal"> </span>
<span class="cbracket">}</span><span class="normal"> </span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
